namespace AltNetUK.Models.Inputs
{
    using System;
    using System.Globalization;

    public sealed class Label
    {
        private FormControl _control;

        public Label(FormControl control, string value)
            : this(control, value, false)
        {
        }

        public Label(FormControl control, string value, bool disabled)
        {
            this.Control = control;
            this.Disabled = disabled;
            this.Value = value;
        }

        public static implicit operator string(Label label)
        {
            return (null == label) ? null as string : label.ToString();
        }

        public FormControl Control
        {
            get
            {
                return this._control;
            }

            set
            {
                if (null == value)
                {
                    throw new ArgumentNullException("value");
                }

                this._control = value;
            }
        }

        public bool Disabled
        {
            get;
            set;
        }

        public string Value
        {
            get;
            set;
        }

        public override string ToString()
        {
            ValidationResult validation = this.Control.Validate(this.Value);

            return string.Format(
                CultureInfo.InvariantCulture,
                "<label for='{0}'{1}{2}>{3}{4}:</label><br />",
                this.Control.Id,
                default(char) == this.Control.AccessKey ? string.Empty : string.Concat(" accesskey='", this.Control.AccessKey ,"'"),
                validation.IsValid ? (this.Disabled ? " class='disabled'" : string.Empty) : " class='error'",
                validation.IsValid ? this.Control.Title : string.Concat(this.Control.Title, " ", validation.Errors[0]),
                this.Control.Required ? string.Empty : " <small><em>(optional)</em></small>"); ;
        }
    }
}